﻿using System;
using System.Management.Automation;

using CrystalDecisions.Enterprise.Desktop;

namespace BO.Management.PowerShell
{

    public class JobServerStatus
    {
        public String Name { get; set; }
        public CrystalDecisions.Enterprise.Desktop.CeExpectedRunState ExpectedState { get; set; }
        public Boolean IsRunning { get; set; }
        public Boolean Enable { get; set; }

    }


    [Cmdlet(VerbsCommon.Get, "JobServerStatus", DefaultParameterSetName = CONNECT_SECWINAD)]
    public class GetJobServerStatus : BaseActionObject<ServerProvider, Server>
    {


        public override ServerProvider GetProvider()
        {
            return new ServerProvider(this.LogonProvider);
        }

        public override void WorkWithSingleObject(Server boeObject)
        {
            if (boeObject != null)
            {

                JobServerStatus status = new JobServerStatus()
                {
                    Name = boeObject.FriendlyName,
                    Enable = !boeObject.Disabled,
                    //ExpectedState = boeObject.ExpectedRunState,
                    IsRunning = boeObject.ServerIsAlive & !boeObject.CurrentDisabledState & !boeObject.Disabled
                };

                try
                {
                    status.ExpectedState = boeObject.ExpectedRunState;
                }
                catch (Exception ex)
                {
                    status.ExpectedState = CeExpectedRunState.NotRunning;
                }


                WriteObject(status);
            }
        }

        public override void WorkWithListObjects(System.Collections.Generic.List<Server> boeObjects)
        {
            foreach (Server server in boeObjects)
            {
                this.WorkWithSingleObject(server);
            }
        }

    }
}
